const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : `0${n}`
}

// 动态加载Anton字体
function loadFont() {
  wx.loadFontFace({
    family: 'Anton',
    source: 'url("https://fonts.gstatic.com/s/anton/v23/1Ptgg87LROyAm3Kz-C8.woff2")',
    desc: {
      style: 'normal',
      weight: 'normal',
      variant: 'normal'
    },
    global: true, // 是否全局生效
    success: (res) => {
      console.log('Anton字体加载成功', res);
      // 触发一个全局事件，通知页面字体已加载
      wx.event = wx.event || {};
      wx.event.emit = wx.event.emit || function(eventName, data) {
        if (!wx.event[eventName]) return;
        wx.event[eventName].forEach(callback => callback(data));
      };
      wx.event.emit('fontLoaded', { family: 'Anton' });
    },
    fail: (err) => {
      console.error('Anton字体加载失败:', err);
    },
    complete: () => {
      console.log('Anton字体加载完成');
    }
  });
}

const convertKeysToLowerCase = obj => {
  if (obj instanceof Array) {
    // 处理数组，只对数组内的对象或Map处理，不改变数组本身的值
    return obj.map(item => {
      if (item instanceof Object && !(item instanceof Map)) {
        return convertKeysToLowerCase(item);
      } else if (item instanceof Map) {
        let newMap = new Map();
        item.forEach((value, key) => {
          let newKey = key.charAt(0).toLowerCase() + key.slice(1);
          newMap.set(newKey, value instanceof Object ? convertKeysToLowerCase(value) : value);
        });
        return newMap;
      }
      return item;
    });
  } else if (obj instanceof Object && !(obj instanceof Map)) {
    // 处理普通对象
    return Object.keys(obj).reduce((result, key) => {
      let newKey = key.charAt(0).toLowerCase() + key.slice(1);
      result[newKey] = obj[key] instanceof Object ? convertKeysToLowerCase(obj[key]) : obj[key];
      return result;
    }, {});
  } else if (obj instanceof Map) {
    // 处理Map
    let newMap = new Map();
    obj.forEach((value, key) => {
      let newKey = key.charAt(0).toLowerCase() + key.slice(1);
      newMap.set(newKey, value instanceof Object ? convertKeysToLowerCase(value) : value);
    });
    return newMap;
  }
  return obj;
}

const unixFormatYYMMDD = timestamp => {
  const date = new Date(timestamp);

  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
  const day = String(date.getDate()).padStart(2, '0');

  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');

  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

module.exports = {
  formatTime,
  loadFont,
  convertKeysToLowerCase,
  unixFormatYYMMDD
}
